package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.redis.RedisKey;
import com.dongdongshop.service.TbUserService;
import com.dongdongshop.util.ShiroUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("tbUser")
public class TbUserController {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private RedisTemplate redisTemplate;
    @Reference
    private TbUserService tbUserService;

    @RequestMapping("register")
    public String register(){
        return "register";
    }

    @RequestMapping("login")
    public String login(){
        return "login";
    }

    @RequestMapping("index")
    public String index(){
        return "login";
    }

    @RequestMapping("getPhone")
    @ResponseBody
    public Result getPhone(String phone){
        if (phone != null && phone != ""){
            try {
                rocketMQTemplate.convertAndSend("phone_code",phone);//审核是否通过,对商家进行同步
                return Result.response(ResultEnum.SUCCESS);
            }catch (Exception e){
                rocketMQTemplate.convertAndSend("phone_code",phone);//审核是否通过,对商家进行同步
                return Result.response(ResultEnum.ERROR);
            }
        }else {
            return Result.response(ResultEnum.NULLPHONE);
        }
    }


    @RequestMapping("unameShow")
    @ResponseBody
    public Result unameShow(String username){
        int i = tbUserService.showAppUname(username);
        if (i == 1){
            return Result.response(ResultEnum.OVERUNAME);
        }else {
            return Result.response(ResultEnum.ERROR);
        }
    }

    @RequestMapping("getRegister")
    @ResponseBody
    public Result showRegister(TbUser tbUser,String code,String password1){
        if (code == null || code == ""){
            return Result.response(ResultEnum.NULLCODE);
        }

        String vcode = (String) redisTemplate.opsForValue().get(RedisKey.PHONENUM_CODE_KEY + tbUser.getPhone());

        if (!code.equals(vcode)){
            return Result.response(ResultEnum.ERRORCODE);
        }
        if (tbUser.getUsername() == null || tbUser.getUsername() == ""){
            return Result.response(ResultEnum.NULLUSER);
        }
        if (tbUser.getPassword() == null || tbUser.getPassword() == ""){
            return Result.response(ResultEnum.NULLPASSWORD);
        }
        if (!tbUser.getPassword().equals(password1)){
            return Result.response(ResultEnum.ERRORTWOPASSWORD);
        }
        String s = ShiroUtils.encryptPassword("MD5", tbUser.getPassword(),"abcd", 10);
        tbUser.setPassword(s);
        tbUser.setCreated(new Date());
        tbUser.setUpdated(new Date());
        int i = tbUserService.showRegister(tbUser);
        if (i <= 0){
            return Result.response(ResultEnum.ERROR);
        }else {
            return Result.response(ResultEnum.SUCCESS);
        }
    }




    @RequestMapping("showTbUser")
    @ResponseBody
    public Result showUser(TbUser user) {
        if (user.getPassword() == null && user.getPassword() == ""){
            return Result.response(ResultEnum.NULLPASSWORD);
        }
        if (user.getUsername() == null && user.getUsername() == ""){
            return Result.response(ResultEnum.NULLUSER);
        }
        //通过调用LoginRealm进行认证
        //1.获取subject对象
        Subject subject = SecurityUtils.getSubject();
        //2.将我们的传来的用户名,密码包,装成传来的shiro的token对象;
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
        //3.调用subject的login方法执行登录逻辑
        try {
            subject.login(token);
        } catch (UnknownAccountException e) {
            return Result.response(ResultEnum.ERROEUSER);
        } catch (IncorrectCredentialsException e) {
            return Result.response(ResultEnum.ERRORPWD);
        }
        return Result.response(ResultEnum.SUCCESS);
    }

}
